Add ability to replace an item in a select view - #768
Conversation
|
Hi, and thanks for the PR! Could I'm fine adding this method, if only for the |
I'm not sure why, but I couldn't seem to get a mutable reference properly where the compiler allowed me to mutate the references. Then I realized that I didn't care about the old values and just need an efficient way to replace an item.
Great idea. I'm happy to make this change.
I like this as it feels consistent with other set_item type APIs. I gave it a shot, but it turns out to be a bit complicated to return the values within the |
Do you mind explaining what you mean by this convenience? I'm not sure I understand this. |
I'd be fine with a
Here's how you can currently get mutable references to the label and content of a let mut v = cursive::views::SelectView::new()
.item_str("Foo")
.item_str("Bar");
let (label, content) = v.get_item_mut(1).unwrap();
*label = "foo".into();
*content = "foooo".into(); Here
Once way is to simply return the pub fn replace_item<S>(&mut self, id: usize, label: S, value: T) -> (StyledString, Arc<T>)
where
S: Into<StyledString>,
{
let prev = std::mem::replace(&mut self.items[id], Item::new(label.into(), value));
self.last_required_size = None;
(prev.label, prev.value)
} Another option is to use |
|
@gyscos I did not disagree with your suggestions in your last comment, but have not had time to address it. I am also moving away from my usage of cursive so I won't have the time in the future. Thank you so much for creating cursive as it was a ton of fun to work with. Feel free to pick up this PR if you find it useful. p.s. my move away from cursive is more so a movement from away from building my TUI app and not a critique of cursive. |
This is a more efficient way to replace an item than calling
remove_item, theninsert_item